import Utils from "../../../../../Utils";
import DestructibleFacility from "../../../../facility/DestructibleFacility";
import { FacilityType, Gain } from "../../../../info/GameEnum";
import { Location, MapLength } from "../../../../info/Predefine";
import Player from "../../../Player";
import { AiEnvironment } from "../../AiGameAction";
import { DecisionNode } from "../../DecisionTree";
import { DecisionAttactUpgradeShellRange } from "./DecisionAttactUpgrade";

/**
 * 攻击敌方设施
 */
export default class DecisionAttackEnemyBase extends DecisionNode {

    environment: AiEnvironment & { sheelScore: number, condition: { shellRange: number; } };

    constructor(environment: any) {
        super(environment);
        this.childDecisionNodes.push(new DecisionAttactUpgradeShellRange(environment));
    }
    /**
     * 我方炮弹数量提供分数
     * @returns 
     */
    evaluate(): number {
        let score = 0;
        score += this.environment.sheelScore;
        return Math.min(100, score);
    }

    execute() {
        //打出去的位置
        let targetLocation: Location = null;

        //如果有敌人威胁的设施
        if (this.environment.enemyThreatValue.size) {
            //按威胁值随机一个敌人
            let enemyArr: Player[] = [];
            let enemythreatValueArr: number[] = [];
            this.environment.enemyThreatValue.forEach((value, key) => {
                if (value > 0) {
                    enemyArr.push(key);
                    enemythreatValueArr.push(value);
                }
            })
            let randomIndex = Utils.getRandomIndexByProbability(enemythreatValueArr);
            let randomEnemy = enemyArr[randomIndex];
            //按设施威胁值 随机一个设施
            let facilityThreatValueMap: Map<DestructibleFacility, number> = this.environment.enemyFacilityThreatValue.get(randomEnemy);
            let facilityArr: DestructibleFacility[] = [];
            let facilitythreatValueArr: number[] = [];
            facilityThreatValueMap.forEach((value, key) => {
                facilityArr.push(key);
                facilitythreatValueArr.push(value);
            })
            randomIndex = Utils.getRandomIndexByProbability(facilitythreatValueArr);
            if(!facilityArr[randomIndex]){
                cc.error("error");
            }
            targetLocation = Utils.getRandomChoose(facilityArr[randomIndex].locations);
        } else {
            //没有就 敌方主基地附近随机打
            let randomLocation = Utils.getRandomChoose(this.environment.enemys).facilityArr[FacilityType.主基地][0].locations[0];
            targetLocation = { locationX: Utils.getRandom(randomLocation.locationX - 5, randomLocation.locationX + 5), locationY: Utils.getRandom(randomLocation.locationY - 5, randomLocation.locationY + 5) }
            targetLocation.locationX = Math.min(MapLength - 1, Math.max(0, targetLocation.locationX));
            targetLocation.locationY = Math.min(MapLength - 1, Math.max(0, targetLocation.locationY));  
        }
        //打出几个炮弹 
        let ampleAmmunition: { count: number, type: FacilityType }[];
        let total = 0,
            current = 0;
        while (ampleAmmunition = this.getAmpleAmmunitionFacility(), ampleAmmunition.length > 0) {
            total++;
            let shell = Utils.getRandomChoose(ampleAmmunition);
            let ret = this.environment.player.dispatchFlyer(shell.type, targetLocation);
            ret && (current += 1);
            // 3/10的几率退出循环
            if (Utils.getRandom(1, 10) <= 3)
                break;
        }

        //射程不足，购买射程
        if (current <= total / 2) {
            if (this.environment.gainFeasibility.get(Gain.炮弹射程升级).every(item => item)) {
                this.environment.condition = {
                    shellRange: 100
                }
                return super.execute();
            }
        }
    }


    /**
     * 获取弹药充足的设施
     */
    getAmpleAmmunitionFacility(): { count: number, type: FacilityType }[] {
        let ret: { count: number, type: FacilityType }[] = [];
        let count = this.environment.player.badgerCount;
        count && ret.push({ count: count, type: FacilityType.平头哥之家 });
        count = this.environment.player.howitzerCount;
        count && ret.push({ count: count, type: FacilityType.榴弹炮基地 });
        count = this.environment.player.rocketCount;
        count && ret.push({ count: count, type: FacilityType.火箭炮基地 });
        count = this.environment.player.electricCount;
        count && ret.push({ count: count, type: FacilityType.电磁炮基地 });
        count = this.environment.player.intercontinentalMissileCount;
        count && ret.push({ count: count, type: FacilityType.洲际导弹基地 });
        count = this.environment.player.nuclearBombCount;
        count && ret.push({ count: count, type: FacilityType.核弹基地 });
        return ret;
    }

}
